#include "xallocator.h"
#include "xsys_log.h"
#include <windows.h>
#include "../ned_alloc/nedmalloc.h"

#ifndef NO_NED_NAMESPACE
#define _malloc nedalloc::nedmalloc
#define _free nedalloc::nedfree
#define _realloc nedalloc::nedrealloc
#else

#define _malloc nedmalloc
#define _free nedfree
#define _realloc nedrealloc
#endif 

namespace xge {
namespace xge_internal {
  void* XMalloc( size_t size ) {
    void* ptr = _malloc(size);
    if( ptr == NULL ) {
#ifdef XGE_USE_WIDE_CHAR
      ::FatalAppExitW( 0 ,
        _xT("Can not malloc heap memory , the memory must be used up!")
        );
#else
      ::FatalAppExitA( 0,
        _xT("Can not malloc heap memory , the memory must be used up!")
        );
#endif
      // Can not reach here 
      abort();
    }

    return ptr;
  }

  void XFree( void* ptr ) {
    _free(ptr);
  }

  void* XRealloc( void* ptr , size_t size ) {
    void* ret = _realloc(ptr,size);
    if( ret == NULL ) {
#ifdef XGE_USE_WIDE_CHAR
      ::FatalAppExitW( 0 ,
        _xT("Can not malloc heap memory , the memory must be used up!")
        );
#else
      ::FatalAppExitA( 0,
        _xT("Can not malloc heap memory , the memory must be used up!")
        );
#endif
      // Can not reach here 
      abort();
    }

    return ret;
  }



}// namespace xge_internal
}// namespace xge

#undef _malloc
#undef _free

